package com.polaris.live.adapter.music

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.polaris.live.R
import com.polaris.live.common.adapter.BaseAdapter
import com.polaris.live.common.ext.autoDp
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.AdapterAddMusicItemBinding
import com.polaris.live.translation.R.string
import com.polaris.live.ui.party.music.SelectMusicBean

/**
 *
 * @author Created by 天晴 on 2024/9/18 11:15
 * @since 1.0.0
 **/
class AddMusicItemAdapter : BaseAdapter<SelectMusicBean, AddMusicItemHolder>(0) {

    private var selectNumListener: (num: Int) -> Unit = {}
    override fun onCreateDefViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): AddMusicItemHolder {
        return AddMusicItemHolder(
            AdapterAddMusicItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        )
    }

    override fun convert(holder: AddMusicItemHolder, item: SelectMusicBean) {
        holder.binding.apply {
            songTv.text = "${StringUtils.getString(string.song_title)}:"
            singerTv.text = "${StringUtils.getString(string.singer)}:"
            musicName.text = item.music.name
            musicPlayer.text = item.music.artist
            musicTime.text = formatTime(item.music.duration)
            Glide.with(context)
                .load(item.music.getAlbumCoverFromMusicFile())
                .placeholder(R.drawable.unknown_cover)
                .error(R.drawable.unknown_cover)
                .transform(CenterCrop(), RoundedCorners(6.autoDp))
                .into(musicPlayerCover)
            setSelect(selectImg, item.select)
            selectImg.setOnSingleClickListener {
                item.select = !item.select
                setSelect(selectImg, item.select)
                notifyItemChanged(holder.absoluteAdapterPosition)
                selectNum()
            }
            if (item.haveSelect) {
                selectImg.isClickable = false
                holder.binding.root.alpha = 0.7f
                selectImg.setImageResource(R.drawable.select_ok)
            }

        }

    }

    private fun setSelect(selectImg: ImageView, select: Boolean) {
        if (select) {
            selectImg.setImageResource(R.drawable.select_ok)
        } else {
            selectImg.setImageResource(R.drawable.select_no)
        }
    }

    private fun selectNum() {
        val selectedCount = data.count { it.select && !it.haveSelect }
        selectNumListener.invoke(selectedCount)
    }

    fun selectNumListener(back: (num: Int) -> Unit) {
        selectNumListener = back
    }

    fun allSelect(pd: Boolean) {
        data.forEach {
            if (!it.haveSelect) {
                it.select = pd
            }
        }
        notifyDataSetChanged()
        selectNumListener.invoke(if (pd) data.count { it.select && !it.haveSelect } else 0)
    }

    private fun formatTime(millis: Long): String {
        val seconds = millis / 1000
        val hours = seconds / 3600
        val minutes = (seconds % 3600) / 60
        val secs = seconds % 60

        return when {
            hours > 0 -> String.format("%02d:%02d:%02d", hours, minutes, secs)
            minutes > 0 -> String.format("%02d:%02d", minutes, secs)
            else -> String.format("00:%02d", secs)
        }
    }
}

class AddMusicItemHolder(
    val binding: AdapterAddMusicItemBinding,
) : BaseViewHolder(binding.root)
